#!/usr/bin/python

"""Project Euler Solution 099

Copyright (c) 2011 by Robert Vella - robert.r.h.vella@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

import cProfile
import math
from euler.file_functions import get_lines
from euler.list_functions import max_by, index

def get_answer():
    """Question:
    
    Comparing two numbers written in index form like 211 and 37 is not 
    difficult, as any calculator would confirm that 211 = 2048 < 37 = 2187.

    However, confirming that 632382**518061 > 519432**525806 would be much more
    difficult, as both numbers contain over three million digits.
    
    Using base_exp.txt, a 22K text file containing one thousand lines with a 
    base/exponent pair on each line, determine which line number has the greatest 
    numerical value.
    
    NOTE: The first two lines in the file represent the numbers in the example 
    given above.
    """
    
    #x > y -> log(x, z) > log(y, z).
    
    #The logarithmic function will also result in smaller numbers.
    
    #Also, because log(x**y) == y*log(x), the expensive exponent operation does
    #not have to be performed.
     
    def get_log_base10(line):
        """Returns the base-10 logarithmic value of the base/exponent pair 
        expressed in [line].
        
        """
        
        #Parse [line].
        elements = line.replace('\n', '').split(',')
        
        #Return the value.
        return math.log10(float(elements[0])) * float(elements[1])
    
    #Return the result.
    return max_by(
                 lambda index_value: index_value.value,
                 index(
                       get_log_base10(line) 
                        for line in get_lines("../data/099.txt")
                    )
                ).index + 1
    
if __name__ == "__main__":
    cProfile.run("print(get_answer())")
